import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.experimental import enable_hist_gradient_boosting
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.ensemble import HistGradientBoostingClassifier

from sklearn.model_selection import GridSearchCV

income = pd.read_excel('../DataSource/adult.xlsx')
income.apply(lambda x: np.sum(x.isnull()))  # 查找数据中的缺失值
income.fillna(value={
    'workclass': income.workclass.mode()[0],
    'occupation': income.occupation.mode()[0],
    'native.country': income['native.country'].mode()[0]

}, inplace=True)
result1 = income.describe()
result2 = income.describe(include=['object'])

plt.style.use('ggplot')
flg, axes = plt.subplots(2, 1)
income.age[income.income == '<=50K'].plot(kind='kde', label='<=50K', ax=axes[0], legend=True, linestyle='-')
income.age[income.income == '>50K'].plot(kind='kde', label='>50K', ax=axes[0], legend=True, linestyle='--')
income['hours.per.week'][income.income == '<=50K'].plot(kind='kde', label='<=50K', ax=axes[1], legend=True,
                                                        linestyle='-')
income['hours.per.week'][income.income == '>50K'].plot(kind='kde', label='>50K', ax=axes[1], legend=True,
                                                       linestyle='--')
plt.show()
race = pd.DataFrame(income.groupby(by=['race', 'income']).aggregate(np.size).loc[:, 'age'])
race = race.reset_index()  # 重建索引
race.rename(columns={'age': 'counts'}, inplace=True)
race.sort_values(by=['race', 'counts'], ascending=False, inplace=True)
plt.figure(figsize=(9, 5))
sns.barplot(x="race", y="counts", hue='income', data=race)
plt.show()

relationship = pd.DataFrame(income.groupby(by=['relationship', 'income']).aggregate(np.size).loc[:, 'age'])
relationship = relationship.reset_index()
relationship.rename(columns={'age': 'counts'}, inplace=True)
relationship.sort_values(by=['relationship', 'counts'], ascending=False, inplace=True)

plt.figure(figsize=(9, 5))
sns.barplot(x="relationship", y="counts", hue='income', data=relationship)

plt.show()

for feature in income.columns:
    if income[feature].dtype == 'object':
        income[feature] = pd.Categorical(income[feature]).codes
result3 = income.head()

income.drop(['education', 'fnlwgt'], axis=1, inplace=True)
result4 = income.head()
X_train, X_test, Y_train, Y_test = train_test_split(income.loc[:, 'age':'native.country'],
                                                    income['income'], train_size=0.75,
                                                    random_state=1234)
print('训练数据集共有%d条观测' % X_train.shape[0])
print('测试数据集共有%d条观测' % X_test.shape[0])

kn = KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski', metric_params=None, n_jobs=1,
                          n_neighbors=5, p=2,
                          weights='uniform')
kn.fit(X_train, Y_train)
print(kn)
#init=None,max_features=None,
gbdt = HistGradientBoostingClassifier( ) #learning_rate=0.1, loss='deviance', max_depth=3,
                                  #  max_leaf_nodes=None, min_samples_leaf=1, min_samples_split=2,
                                  # min_weight_fraction_leaf=0.0, n_estimators=100, presort='auto', random_state=None,
                                  # subsample=1.0,
                                  # verbose=0, warm_start=False
gbdt.fit(X_train, Y_train)
print(gbdt)

k_options = list(range(1, 12))
parameters = {'n_nelghbors': k_options}
grid_kn = GridSearchCV(estimator=KNeighborsClassifier(), param_grid=parameters, cv=10, scoring='accuracy')
grid_kn.fit(X_train, Y_train)
print(grid_kn.grid_scores_, grid_kn.best_params_, grid_kn.best_score_)
